import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();

        Student stu1 = new Student(1, "a", 1);
        Student stu2 = new Student(2, "b", 2);

        list.add(stu1);
        list.add(stu2);

        Student stu3 = new Student(3, "c", 3);
        boolean flag = contains(list, stu3.getId());
        if (flag) {
            System.out.println("当前id重复，请修改id后再进行添加");
        } else {
            list.add(stu3);
            printArr(list);
        }
    }
    public static void printArr(ArrayList list){
        for (int i=0;i<list.size();i++){
            Student stu = (Student)list.get(i);
            System.out.println("学号："+stu.getId() + " 名字："+stu.getName() + " 年龄："+stu.getAge());
        }
    }

    public static boolean contains(ArrayList list, int id) {
        for (int i = 0; i < list.size(); i++) {
            Student stu = (Student) list.get(i);
            if (stu != null) {
                //获取数组中学生对象的id
                int sid = stu.getId();
                //比较
                if (sid == id) {
                    return true;
                }
            }

        }//当循环结束之后，还没有找到一样的，那么就表示数组中要查找的id是不存在的。
        return false;
    }
}
